package com.example.adms.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adms.role.dto.SetPermParams;
import com.example.adms.role.entity.Permission;
import com.example.adms.role.entity.RolePermission;
import com.example.adms.role.mapper.PermissionMapper;
import com.example.adms.role.mapper.RolePermissionMapper;
import com.example.adms.role.service.PermissionService;
import com.example.adms.role.vo.PermissionVo;
import com.example.adms.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 获取权限列表
     * @return
     */
    @Override
    public Result getPermList(Integer roleId) {
        if (ObjectUtils.isEmpty(roleId)){
            //没有传roleId查全部
            List<Permission> list = this.list();
            List<PermissionVo> voList = new ArrayList<>();
            for (Permission perm: list){
                PermissionVo permissionVo = new PermissionVo();
                BeanUtils.copyProperties(perm,permissionVo);
                voList.add(permissionVo);
            }
            return Result.success(voList);
        } else {
            //有roleId根据roleId查询
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",roleId);
            List<PermissionVo> voList = rolePermissionMapper.selectPermListByRoleId(map);
            return Result.success(voList);
        }

    }

    /**
     * 设置角色权限
     * @param setPermParams
     * @return
     */
    @Override
    @Transactional
    public Result setPermByRoleId(SetPermParams setPermParams) {
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",setPermParams.getRoleId());
        rolePermissionMapper.deleteByRoleId(map);
        map.put("permList",setPermParams.getPermList());
        rolePermissionMapper.insertPerms(map);
        return Result.success(null);
    }

    /**
     * 根据角色id获取拥有的权限
     * @param roleId
     * @return
     */
    @Override
    public Result getPermListByRoleId(Integer roleId) {
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId);
        List<PermissionVo> voList = rolePermissionMapper.selectPermListByRoleId(map);
        List<String> list = new ArrayList<>();
        for (PermissionVo vo : voList){
            list.add(vo.getPermissionName());
        }
        Object[] array = list.toArray();
        return Result.success(array);
    }
}
